<?php
	//Define Variables ++++++++++++++++++++++++++++++++++++++++

		$MySQL_Host = "localhost";
		$MySQL_User_Name = "root";
		$MySQL_Password = "";
		$MySQL_Database = "Traffic-Lite";



	$menuItems = array("Grid","Program Log","Help Desk","FAQ","Manual","Admin");//Main Menu items for index page (Used to add more modules)
	$pageConent = array("grid.php","log.php","coming.php","coming.php","coming.php","admin.php");//corresponding content for each menu item in $menuItems (Used to add more modules)
	$colorTheme = "#3A6A74";//color for cell backgrounds
	$highLightFont="#BFD0CB";//color of font for titles etc.
	$activeSectionFont = "#000000";//color of non-link sections you are currently in
	$BGColor = "#BFD0CB";

	//pear info
	$dsn = "mysql://$MySQL_User_Name:$MySQL_Password@$MySQL_Host/$MySQL_Database";


	//############################################################
	//BEGIN GLOBAL FUNCTIONS AND DATA#############################
	//############################################################
		require_once('PEAR.php');
		require_once ('DB.php');
		require_once 'Date.php';
		require_once 'Date/Span.php';
		require_once 'Date/Calc.php';


		$db = DB::connect($dsn);
		if (DB::isError($db)) {
   		die ($db->getMessage());
		}


		//Days of week array
		$daysOfWeek = array("mon","tue","wed","thu","fri","sat","sun");




		//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
		//BEGIN FUNCTION TO DETERMINE NEAREST MONDAYS TIME STAMP
		//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
		function findMonday($timestamp){
			$month = Date_Calc::prevDayOfWeek(1,date("d",$timestamp),date("m",$timestamp),date("Y",$timestamp),$format='%m',$onOrBefore=true);
			$day = Date_Calc::prevDayOfWeek(1,date("d",$timestamp),date("m",$timestamp),date("Y",$timestamp),$format='%d',$onOrBefore=true);
			$year = Date_Calc::prevDayOfWeek(1,date("d",$timestamp),date("m",$timestamp),date("Y",$timestamp),$format='%Y',$onOrBefore=true);
			$_SESSION['nearestMonday'] = mktime(0,0,0,$month,$day,$year);//finds the nearest Monday of cuurent week
		}



		//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
		//BEGIN FUNCTION TO DETERMINE ACCURATE START AND END TIMESTAMPS
		//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
		function determineTimeStamps($dow,$start,$end){//Day of week you want a time stamp for, start time stamp, end timestamp
				//Determine start time stamp
				$month = Date_Calc::nextDayOfWeekOnOrAfter($dow,date("d",$start),date("m",$start),date("Y",$start),$format='%m');
				$day = Date_Calc::nextDayOfWeekOnOrAfter($dow,date("d",$start),date("m",$start),date("Y",$start),$format='%d');
				$year = Date_Calc::nextDayOfWeekOnOrAfter($dow,date("d",$start),date("m",$start),date("Y",$_SESSION['timestamp']),$format='%Y');
				$_SESSION['AccurateStartStamp'] = mktime(0,0,0,$month,$day,$year);

				//Determine end time stamp
				if($end == 'NA'){
					$_SESSION['AccurateEndStamp'] = 'NA';
				}else{
					//find nearest monday for the week of given end timestamp
					$month = Date_Calc::prevDayOfWeek(1,date("d",$end),date("m",$end),date("Y",$end),$format='%m',$onOrBefore=true);
					$day = Date_Calc::prevDayOfWeek(1,date("d",$end),date("m",$end),date("Y",$end),$format='%d',$onOrBefore=true);
					$year = Date_Calc::prevDayOfWeek(1,date("d",$end),date("m",$end),date("Y",$end),$format='%Y',$onOrBefore=true);
					$weekStartStamp = mktime(0,0,0,$month,$day,$year);//finds the nearest Monday of cuurent week

					//Determine end time stamp
					$month = Date_Calc::nextDayOfWeekOnOrAfter($dow,date("d",$weekStartStamp),date("m",$weekStartStamp),date("Y",$weekStartStamp),$format='%m');
					$day = Date_Calc::nextDayOfWeekOnOrAfter($dow,date("d",$weekStartStamp),date("m",$weekStartStamp),date("Y",$weekStartStamp),$format='%d');
					$year = Date_Calc::nextDayOfWeekOnOrAfter($dow,date("d",$weekStartStamp),date("m",$weekStartStamp),date("Y",$weekStartStamp),$format='%Y');
					$_SESSION['AccurateEndStamp'] = mktime(0,0,0,$month,$day,$year);
				}
		}
		//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
		//END FUNCTION TO DETERMINE ACCURATE START AND END TIMESTAMPS
		//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++




		//THIS FUNCTION IS DEPRICATED!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
		//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
		//BEGIN FUNCTION TO DETERMINE ACCURATE START AND END TIMESTAMPS
		//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
		function DetermineAccurateTimeStamps($startTime,$endTime,$firstDayID,$lastDayID,$NextHighestDay,$NextLowestDay,$NumberOfDaysUsed,$FormStartDayID,$FormEndDayID){
			//FIND ACCURATE END TIME
			if($endTime != 'NA'){
					global $daysOfWeek;
					if($FormEndDayID > $lastDayID){
						$SubtractAmountFromEndTime = ($FormEndDayID - $lastDayID) * 86400;
						$_SESSION['accurateEndTime'] = $endTime - $SubtractAmountFromEndTime;
					}else if($FormEndDayID < $lastDayID){

						if($NumberOfDaysUsed > 1 && $NextLowestDay){//figure out the next day that is lower than the end date
							$_SESSION['accurateEndTime'] = $endTime -(($FormEndDayID - $NextLowestDay) * 86400);
						}else{ //go back to previous week for end date
							$_SESSION['accurateEndTime'] = $endTime - (((7 - $lastDayID) + $FormEndDayID) * 86400);
						}
					}else{
						$_SESSION['accurateEndTime'] = $endTime;
					}
			}else{
				$_SESSION['accurateEndTime'] = 'NA';
			}



			//FIND ACCURATE START TIME
			if($FormStartDayID < $firstDayID){
				$AddToStartTime = ($firstDayID - $FormStartDayID) * 86400;
				$_SESSION['accurateStartTime'] = $startTime + $AddToStartTime;
			}else if($FormStartDayID > $firstDayID){

				if($NumberOfDaysUsed > 1 && $NextHighestDay){//figure out the next day that is lower than the end date
					$_SESSION['accurateStartTime'] = $startTime + (($NextHighestDay - $FormStartDayID) * 86400);
				}else{ //go forward to next week for start date
					$_SESSION['accurateStartTime'] = $startTime + (((7 - $FormStartDayID) + $firstDayID) * 86400);
				}
			}else{
				$_SESSION['accurateStartTime'] = $startTime;
			}

		}
		//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
		//END FUNCTION TO DETERMINE ACCURATE START AND END TIMESTAMPS
		//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++


		//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
		//BEGIN FUNCTION TO SORT ARRAYS BY A PARTICULAR KEY++++++++++++++++++
		//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
class array_sorter
{
   var $skey = false;
   var $sarray = false;
   var $sasc = true;


   function array_sorter(&$array, $key, $asc=true)
   {
       $this->sarray = $array;
       $this->skey = $key;
       $this->sasc = $asc;
   }


   function sortit($remap=true)
   {
       $array = &$this->sarray;
       uksort($array, array($this, "_as_cmp"));
       if ($remap)
       {
           $tmp = array();
           while (list($id, $data) = each($array))
               $tmp[] = $data;
           return $tmp;
       }
       return $array;
   }


   function _as_cmp($a, $b)
   {
       //since uksort will pass here only indexes get real values from our array
       if (!is_array($a) && !is_array($b))
       {
           $a = $this->sarray[$a][$this->skey];
           $b = $this->sarray[$b][$this->skey];
       }

       //if string - use string comparision
       if (!ctype_digit($a) && !ctype_digit($b))
       {
           if ($this->sasc)
               return strcasecmp($a, $b);
           else
               return strcasecmp($b, $a);
       }
       else
       {
           if (intval($a) == intval($b))
               return 0;

           if ($this->sasc)
               return (intval($a) > intval($b)) ? -1 : 1;
           else
               return (intval($a) > intval($b)) ? 1 : -1;
       }
   }

}//end of class





   function multi_sort(&$array, $key, $asc=true)
   {
       $sorter = new array_sorter($array, $key, $asc);
       return $sorter->sortit();
   }	//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
		//END FUNCTION TO SORT ARRAYS BY A PARTICULAR KEY++++++++++++++++++++
		//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++


		//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
		//BEGIN FUNCTION TO CALCULATE MILITARY TIME++++++++++++++++++++++++++
		//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

		function calcMilitary($hour,$minute,$seconds,$ampm){
				if($ampm == 'AM'){
					if($hour == '12'){
					$military = "00:$minute:$seconds";
					}else if($hour == '1'){
					$military = "01:$minute:$seconds";
					}else if($hour == '2'){
					$military = "02:$minute:$seconds";
					}else if($hour == '3'){
					$military = "03:$minute:$seconds";
					}else if($hour == '4'){
					$military = "04:$minute:$seconds";
					}else if($hour == '5'){
					$military = "05:$minute:$seconds";
					}else if($hour == '6'){
					$military = "06:$minute:$seconds";
					}else if($hour == '7'){
					$military = "07:$minute:$seconds";
					}else if($hour == '8'){
					$military = "08:$minute:$seconds";
					}else if($hour == '9'){
					$military = "09:$minute:$seconds";
					}else if($hour == '10'){
					$military = "10:$minute:$seconds";
					}else if($hour == '11'){
					$military = "11:$minute:$seconds";
					}
				}else{
					if($hour == '12'){
					$military = "12:$minute:$seconds";
					}else if($hour == '01'){
					$military = "13:$minute:$seconds";
					}else if($hour == '02'){
					$military = "14:$minute:$seconds";
					}else if($hour == '03'){
					$military = "15:$minute:$seconds";
					}else if($hour == '04'){
					$military = "16:$minute:$seconds";
					}else if($hour == '05'){
					$military = "17:$minute:$seconds";
					}else if($hour == '06'){
					$military = "18:$minute:$seconds";
					}else if($hour == '07'){
					$military = "19:$minute:$seconds";
					}else if($hour == '08'){
					$military = "20:$minute:$seconds";
					}else if($hour == '09'){
					$military = "21:$minute:$seconds";
					}else if($hour == '10'){
					$military = "22:$minute:$seconds";
					}else if($hour == '11'){
					$military = "23:$minute:$seconds";
					}

				}

				$GLOBALS["military"] = $military;
		}
		//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
		//END FUNCTION TO CALCULATE MILITARY TIME++++++++++++++++++++++++++++
		//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++







	//############################################################
	//END GLOBAL FUNCTIONS AND DATA###############################
	//############################################################
?>
